﻿using MVVM.NotifyPropertyChanged.Commands;
using MVVM.NotifyPropertyChanged.Services;
using MVVM.NotifyPropertyChanged.Views;

namespace MVVM.NotifyPropertyChanged.ViewModels
{
    public class LoginViewModel : NotifyProperty
    {
        private readonly IUserService _userService;
        private string username;

        public string Username
        {
            get { return username; }
            set { username = value; OnPropertyChanged(); }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; OnPropertyChanged(); }
        }

        public ICommand<string, bool> LoginCommand { get; }
        public LoginViewModel(IUserService userService)
        {
            _userService = userService;
            LoginCommand = new RelayCommand<string, bool>(Login);
        }

        private bool Login(string name)
        {
            var user = _userService.GetUserByUsername(Username);
            if (user != null && user.Password == Password)
            {
                MainView mainForm = new MainView();
                mainForm.Show();
                return true;
            }
            else
            {
                // 显示登录失败的消息
                MessageBox.Show("用户名密码错误");
                return false;
            }
        }
    }
}
